package com.cropdoctor.controller;

import com.alibaba.fastjson.JSONObject;
import com.cropdoctor.common.Result;
import com.cropdoctor.model.dto.PwdFormDto;
import com.cropdoctor.model.po.SysUserPo;
import com.cropdoctor.model.vo.UserVO;
import com.cropdoctor.service.SysUserService;
import com.cropdoctor.service.WeChatService;
import io.swagger.annotations.Api;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;

@RestController
@RequestMapping("/auth")
@Api(tags = "账号管理相关接口")
public class UserLoginController {
    @Autowired
    WeChatService weChatService;

    @Autowired
    SysUserService sysUserService;

    @PostMapping("/wx/login") //wx验证方式 基于表单验证无需提供controller springsecurity已经封装好了
    public Result<SysUserPo> login(@RequestBody JSONObject userInfo){
        return weChatService.login(userInfo);
    }

    @GetMapping("/userinfo")
    public Result<UserVO> getUserInfo(HttpServletRequest request){
        return sysUserService.getUserInfo(request);
    }

    @PutMapping("/userinfo")
    public Result<String> updateUserName(String userName){
        return sysUserService.updateUserName(userName);
    }

    @PostMapping("/avatar")
    public Result<String> updateAvatar(@RequestParam("img") MultipartFile img){
        return sysUserService.updateAvatar(img);
    }

    @PutMapping("/pwd")
    public Result<String> updatePwd(@RequestBody PwdFormDto pwdFormDto){
        return sysUserService.updatePwd(pwdFormDto);
    }

    @PutMapping("/sign")
    public Result<String> dailySign(){
        return sysUserService.dailySign();
    }

    @GetMapping("/sign")
    public Result<Integer> getSign(){
        return sysUserService.getSign();
    }
}
